{% extends "base/admin.html" %}

{% block title %}插件管理{% endblock %}

{% block head %}
    <!-- Data Tables -->
    <link href="/static/admin/css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
{% endblock %}

{% block content %}
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>插件管理</h5><small><a href="javascript:ReloadPlugins()">&nbsp;<i class="fa fa-refresh fa-spin" aria-hidden="true"></i>重新扫描并加载插件</a></small>
                    </div>
                    <div class="ibox-content">
                        <table class="table table-striped table-bordered table-hover dataTables-plugins">
                            <thead>
                                <tr>
                                    <th>名称</th>
                                    <th>描述</th>
                                    <th>版本</th>
                                    <th>作者</th>
                                    <th>链接</th>
                                    <th>许可证</th>
                                    <th>状态</th>
                                    <th>动作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for plugin in Plugins %}
                                <tr>
                                    <td>{{ plugin.plugin_name }}</td>
                                    <td>{{ plugin.plugin_description }}</td>
                                    <td>{{ plugin.plugin_version }}</td>
                                    <td>{{ plugin.plugin_author }}</td>
                                    <td>{{ plugin.plugin_url }}</td>
                                    <td>{{ plugin.plugin_license }}</td>
                                    <td>{{ plugin.plugin_state }}</td>
                                    <td class="center">
                                        {% if plugin.plugin_state == "enabled" %}
                                            <a class="btn btn-sm btn-danger btn-rounded btn-outline" href="javascript:DisablePlugin('{{ plugin.plugin_name }}')"><strong>禁用</strong></a><br>
                                        {% else %}
                                            <a class="btn btn-sm btn-success btn-rounded btn-outline" href="javascript:EnablePlugin('{{ plugin.plugin_name }}')"><strong>启用</strong></a><br>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
{% endblock %}


{% block script %}
    <!-- Data Tables -->
    <script src="/static/admin/js/plugins/dataTables/jquery.dataTables.js"></script>
    <script src="/static/admin/js/plugins/dataTables/dataTables.bootstrap.js"></script>
    <!-- Page-Level Scripts -->
    <script>
        $(document).ready(function () {
            $('.dataTables-plugins').dataTable();

            /* Init DataTables */
            var oTable = $('#editable').dataTable();
        });
        // 重载插件
        function ReloadPlugins() {
            $.ajax({
                url: '{{ url_for("api.plugin", action="reload_plugin") }}',
                method: 'POST',
                datatype: 'json',
                success: function(res) {
                    console.log(res);
                    if (res.success==true) {
                        layer.msg("重载成功", {icon: 1, time: 3*1000});
                        parent.location.reload();
                    } else {
                        layer.alert("重载失败", {icon: 2, time: 15*1000});
                    }
                },
                error:function(e){
                    layer.alert("服务器错误，请刷新重试或报告错误！", {icon: 2, time: 15*1000});
                }
            });
        };
        // 启用插件
        function EnablePlugin(plugin_name) {
            $.ajax({
                url: '{{ url_for("api.plugin", action="enable_plugin") }}',
                method: 'POST',
                datatype: 'json',
                data: {"plugin_name": plugin_name},
                success: function(res) {
                    console.log(res);
                    if (res.success==true) {
                        layer.msg("启用插件成功", {icon: 1, time: 3*1000});
                        parent.location.reload();
                    } else {
                        layer.alert("启用插件失败，请重试提交或报告错误！\n错误原因是：" + res.msg, {icon: 2, time: 15*1000});
                    }
                },
                error:function(e){
                    layer.alert("服务器错误，请刷新重试或报告错误！", {icon: 2, time: 15*1000});
                }
            });
        };
        // 禁用插件
        function DisablePlugin(plugin_name) {
            $.ajax({
                url: '{{ url_for("api.plugin", action="disable_plugin") }}',
                method: 'POST',
                datatype: 'json',
                data: {"plugin_name": plugin_name},
                success: function(res) {
                    console.log(res);
                    if (res.success==true) {
                        layer.msg("禁用插件成功", {icon: 1, time: 3*1000});
                        parent.location.reload();
                    } else {
                        layer.alert("禁用插件失败，请重试提交或报告错误！\n错误原因是：" + res.msg, {icon: 2, time: 15*1000});
                    }
                },
                error:function(e){
                    layer.alert("服务器错误，请刷新重试或报告错误！", {icon: 2, time: 15*1000});
                }
            });
        };
    </script>
{% endblock %}
